package com.zut.demo1.controller;

import com.zut.demo1.model.Role;
import com.zut.demo1.service.RoleService;
import com.zut.demo1.utils.Msg;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/login")
    public Msg userInfo(HttpServletRequest request, HttpSession session) {
        String cn = "";
        String uid = "";
        String root = "";
        Principal principal = request.getUserPrincipal();
        if (principal != null && principal instanceof AttributePrincipal) {
            AttributePrincipal aPrincipal = (AttributePrincipal) principal;
            //获取用户信息中公开的Attributes部分
            Map<String, Object> map = aPrincipal.getAttributes();
            // 获取姓名,可以根据属性名称获取其他属性
            for(String key : map.keySet()) {
                System.out.println(key + ":" + map.get(key));
            }
            cn = (String) map.get("cn");
            uid = (String) map.get("uid");

            Role role1 = new Role();

            role1.setRoleName(cn);
            role1.setRoleKey(uid);

            root = roleService.getRoot(role1);

        }
        return Msg.success().add("role",root);
    }

}
